package com.imooc.sell.controller;

import com.imooc.sell.VO.ProductInfoVO;
import com.imooc.sell.VO.ProductVO;
import com.imooc.sell.VO.ResultVO;
import com.imooc.sell.dataobject.ProductCategory;
import com.imooc.sell.dataobject.ProductInfo;
import com.imooc.sell.service.CategoryService;
import com.imooc.sell.service.ProductService;
import com.imooc.sell.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    @RequestMapping("/list")
    public ResultVO list(){
        ArrayList<ProductVO> productVOS = new ArrayList<ProductVO>();

        List<ProductInfo> productInfoList = productService.findUpAll();
        List<ProductCategory> categoryList = categoryService.findAll();



        for (ProductCategory productCategory : categoryList) {
            ArrayList<ProductInfoVO> productInfoVOS = new ArrayList<ProductInfoVO>();
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());

            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);

                    productInfoVOS.add(productInfoVO);
                }
            }

            productVO.setProductInfoVOList(productInfoVOS);
            productVOS.add(productVO);
        }
        ResultVO resultVO = ResultVOUtil.success(productVOS);
        return  resultVO;
    }
}
